﻿Partial Public Class GeoWallpaperUserControl
    Inherits UserControl
    Public Sub New()
        InitializeComponent()
        btnCompleteAuth.Visibility = Windows.Visibility.Collapsed
    End Sub

    Private Sub UserControl_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
    End Sub

    Private Function SetError(ByVal control As Control, ByVal msg As String) As Boolean
        MessageBox.Show(msg, "Error", MessageBoxButton.OK, MessageBoxImage.[Error])
        control.Focus()
        Return False
    End Function

    Private FindNearbyImages_Lock As New Object()

    Private Sub btnUpdateTags_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        btnUpdateTags.IsEnabled = False

        Dispatcher.Invoke(Function() UpdateTagsBackground())
    End Sub

    Private Function UpdateTagsBackground()
        DirectCast(Me.DataContext, GeoWallpaperAddinImpl).FindNearbyImages()
        DirectCast(Me.DataContext, GeoWallpaperAddinImpl).UpdateWallpaper()
        btnUpdateTags.IsEnabled = True
        Return Nothing
    End Function

    Private Sub btnRefresh_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        btnRefresh.IsEnabled = False

        Dispatcher.Invoke(Function() UpdateWallpaperBackground())
    End Sub

    Private Function UpdateWallpaperBackground()
        DirectCast(Me.DataContext, GeoWallpaperAddinImpl).UpdateWallpaper()
        btnRefresh.IsEnabled = True
        Return Nothing
    End Function

    Private Sub btnUpdateInterval_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        DirectCast(Me.DataContext, GeoWallpaperAddinImpl).UpdateInterval()
    End Sub

    Private Sub btnBeginAuth_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        DirectCast(Me.DataContext, GeoWallpaperAddinImpl).BeginUserAuth()
        btnCompleteAuth.Visibility = Windows.Visibility.Visible
    End Sub

    Private Sub btnCompleteAuth_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        DirectCast(Me.DataContext, GeoWallpaperAddinImpl).CompleteUserAuth()
        btnCompleteAuth.Visibility = Windows.Visibility.Collapsed
    End Sub

    Private Sub cbAutoUpdate_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles cbAutoUpdate.Click
        DirectCast(Me.DataContext, GeoWallpaperAddinImpl).UpdateInterval()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click
        System.Diagnostics.Process.Start(DirectCast(Me.DataContext, GeoWallpaperAddinImpl).LastPhoto.WebUrl)
    End Sub
End Class
